package com.fengwk.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fengwk.support.util.ResultUtils;
import com.fengwk.user.face.model.LoginSession;
import com.fengwk.user.service.LoginService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "登录")
@RestController
@RequestMapping("/api/login")
public class LoginController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private LoginService loginService;
	
	@ApiOperation(
			value = "通过通道和令牌获取登录会话", 
			response = LoginSession.class)
	@PostMapping("/getLoginSessionByChannelAndToken")
	public void getLoginSessionByChannelAndToken(
			@ApiParam(value = "通道", required = true) @RequestParam(value = "channel", required = true) String channel,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "token", required = true) String token) {
		ResultUtils.returnJsonResult(request, response, loginService.getLoginSessionByChannelAndToken(channel, token));
	}
	
	@ApiOperation(
			value = "通过通道、身份、凭证登录", 
			response = LoginSession.class)
	@PostMapping("/login")
	public void  login(
			@ApiParam(value = "通道", required = true) @RequestParam(value = "channel", required = true) String channel,
			@ApiParam(value = "身份", required = true) @RequestParam(value = "identity", required = true) String identity, 
			@ApiParam(value = "凭证", required = true) @RequestParam(value = "certification", required = true) String certification) {
		ResultUtils.returnJsonResult(request, response, loginService.login(channel, identity, certification));
	}
	
	@ApiOperation(
			value = "通过通道和令牌登出", 
			response = Void.class)
	@PostMapping("/logout")
	void logout(
			@ApiParam(value = "通道", required = true) @RequestParam(value = "channel", required = true) String channel,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "token", required = true) String token) {
		loginService.logout(channel, token);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
}
